<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>主页</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
    <?php include 'includes/navbar.php'; ?>
    <div class="container mt-5">
        <h1 class="mb-4">欢迎来到小工具主页</h1>
        <div class="row" id="toolsContainer">
            <!-- 小工具内容 -->
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="js/script.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            fetchTools();
        });

        function fetchTools() {
            fetch('admin/get-tools.php')
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        var toolsContainer = document.getElementById('toolsContainer');
                        toolsContainer.innerHTML = '';
                        data.tools.forEach(function(tool) {
                            if (tool.status === 1) {
                                var toolCard = `
                                    <div class="col-md-6">
                                        <div class="card mb-4">
                                            <div class="card-header">
                                                ${tool.name}
                                            </div>
                                            <div class="card-body">
                                                <p>${tool.description}</p>
                                                <p>调用价格: ${tool.price} 元/次</p>
                                                <button type="button" class="btn btn-primary" onclick="useTool(${tool.id})">使用工具</button>
                                            </div>
                                        </div>
                                    </div>
                                `;
                                toolsContainer.innerHTML += toolCard;
                            }
                        });
                    } else {
                        document.getElementById('toolsContainer').innerHTML = '<p>暂无可用小工具</p>';
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                });
        }

        function useTool(toolId) {
            if (!sessionStorage.getItem('isLoggedIn')) {
                window.location.href = 'login.html';
                return;
            }

            // 根据工具ID调用相应的API
            switch (toolId) {
                case 1:
                    window.location.href = 'verify.html';
                    break;
                case 2:
                    window.location.href = 'mobile-verify.html';
                    break;
                case 3:
                    window.location.href = 'xmbx.html';
                    break;
                case 4:
                    window.location.href = 'remove-watermark.html';
                    break;
                default:
                    alert('工具不存在');
            }
        }
    </script>
</body>
</html>